#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>

const unsigned int kWidth = 800;
const unsigned int kHeight = 600;

/*
					x		y
hull center			0		0
hull front left		-31,42	80,30
hull front right	31,42	80,30
hull rear left		-31,42	-37,41
hull rear right		31,42	-37,41
turret center		0,00	14,65
turret front left	-26,40	53,40
turret front right	26,40	53,40
turret rear right	26,40	-12,45
turret rear left	-26,40	-12,45
gun front left		-1,61	124,67
gun front right		1,61	124,67
gun rear left		-1,61	53,40
gun rear right		1,61	53,40
*/

// turret length 65,85
// hul length 138/680
// 797 - 680

struct aPoint {
	float x;
	float y;
};

struct aMesh { //4
	aPoint fl; // front left
	aPoint fr; // front right
	aPoint rl; // rear left
	aPoint rr; // rear right
};


struct aTurret { //1 + 2 * 4
	aPoint center;
	// {0.00, 14.65}
	aMesh turret;
	//{{-26.40, 53.40},{26.40, 53.40},{-26.40, -12.45},{26.40, -12.45}}
	aMesh gun;
	//{{-1.61, 124.67},{1.61, 124.67},{-1.61, 53.40},{1.61, 53.40}}
};

struct aTank {//1 + 4 + 9
	aPoint center;
	aMesh hull;
	//{{-31.42, 80.30},{31.42, 80.30},{-31.42, -37,41},{31.42, -37,41}}
	aTurret turret;
};

const aTank abrams = {
	{0, 0}, // center
	{// hull
		{-31.42, 80.30},{31.42, 80.30},{-31.42, -37.41},{31.42, -37.41}
	},
	{ // turret
		{0.00, 14.65}, // center
		{ // turret
			{-26.40, 53.40},{26.40, 53.40},{-26.40, -12.45},{26.40, -12.45}
		},
		{ // gun
			{-1.61, 124.67},{1.61, 124.67},{-1.61, 53.40},{1.61, 53.40}
		}
	}
};

aPoint operator + (aPoint &a, aPoint &b) {
	aPoint result;
	result.x = a.x + b.x;
	result.y = a.y + b.y;

	return result;
}

void operator += (aPoint &a, const aPoint &b) {
	a.x += b.x;
	a.y += b.y;
}

void operator += (aTank &tank, const aPoint &a) {
	aPoint *points = (aPoint*)&tank;
	char count = sizeof(aTank) / sizeof(aPoint);
	for ( int i = 0; i < count; i++ ) {
		points[ i] += a;
	}
}

void moveTank( aTank &tank, const aPoint &x ) {
	tank += x;
}

void drawTank() {
	const aPoint center = {kWidth/2, kHeight/2};
	aTank myTank = abrams;
	ALLEGRO_COLOR white = al_map_rgb( 255, 255, 255 );

	moveTank( myTank, center );

	// draw hull
	al_draw_line( myTank.hull.fl.x, myTank.hull.fl.y, myTank.hull.fr.x, myTank.hull.fr.y, white, 2 );
	al_draw_line( myTank.hull.rl.x, myTank.hull.rl.y, myTank.hull.rr.x, myTank.hull.rr.y, white, 2 );
	al_draw_line( myTank.hull.fl.x, myTank.hull.fl.y, myTank.hull.rl.x, myTank.hull.rl.y, white, 2 );
	al_draw_line( myTank.hull.fr.x, myTank.hull.fr.y, myTank.hull.rr.x, myTank.hull.rr.y, white, 2 );
	// draw turret
	al_draw_line( myTank.turret.turret.fl.x, myTank.turret.turret.fl.y, myTank.turret.turret.fr.x, myTank.turret.turret.fr.y, white, 1 );
	al_draw_line( myTank.turret.turret.rl.x, myTank.turret.turret.rl.y, myTank.turret.turret.rr.x, myTank.turret.turret.rr.y, white, 1 );
	al_draw_line( myTank.turret.turret.fl.x, myTank.turret.turret.fl.y, myTank.turret.turret.rl.x, myTank.turret.turret.rl.y, white, 1 );
	al_draw_line( myTank.turret.turret.fr.x, myTank.turret.turret.fr.y, myTank.turret.turret.rr.x, myTank.turret.turret.rr.y, white, 1 );
	// draw gun
	al_draw_line( myTank.turret.gun.fl.x, myTank.turret.gun.fl.y, myTank.turret.gun.fr.x, myTank.turret.gun.fr.y, white, 1 );
	al_draw_line( myTank.turret.gun.rl.x, myTank.turret.gun.rl.y, myTank.turret.gun.rr.x, myTank.turret.gun.rr.y, white, 1 );
	al_draw_line( myTank.turret.gun.fl.x, myTank.turret.gun.fl.y, myTank.turret.gun.rl.x, myTank.turret.gun.rl.y, white, 1 );
	al_draw_line( myTank.turret.gun.fr.x, myTank.turret.gun.fr.y, myTank.turret.gun.rr.x, myTank.turret.gun.rr.y, white, 1 );
	//al_draw_line(float x1, float y1, float x2, float y2,
//ALLEGRO_COLOR color, float thickness)
}
 
int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *display = NULL;
 
   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }

   al_init_primitives_addon();
 
   // Display setup
   al_set_new_display_flags( ALLEGRO_WINDOWED );
   display = al_create_display(kWidth, kHeight);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      return -1;
   }
 
   al_clear_to_color(al_map_rgb(0,0,0));
    
   // ==========================
   drawTank();
   // ==========================
 
   al_flip_display();
 
   al_rest(10.0);
 
   al_destroy_display(display);
 
   return 0;
}